package Gui;

//Test

import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.Event;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;
import java.awt.Dimension;
import javax.swing.JSlider;
import javax.swing.Timer;

import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import java.awt.SystemColor;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
import javax.swing.JButton;

import Logic.Ball;
import Logic.CircleDraw;
import javax.swing.*;

public class frontend_main {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu editMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuItem cutMenuItem = null;
	private JMenuItem copyMenuItem = null;
	private JMenuItem pasteMenuItem = null;
	private JMenuItem saveMenuItem = null;
	private JFrame consoleFrame = null;  //  @jve:decl-index=0:visual-constraint="504,10"
	private JPanel aboutContentPane = null;
	private CircleDraw ballPanel = null;
	private JButton add00 = null;
	private JButton add01 = null;
	private JButton add10 = null;
	private JButton add11 = null;
	private Graphics2D g2;  //  @jve:decl-index=0:
	private ArrayList<Ball> ballList = new ArrayList<Ball>();  //  @jve:decl-index=0:
	private JButton clearButton = null;
	public static JTextArea consoleArea = null;
	private JButton showConsoleBtn = null;
	
	private boolean inside = false;
	 //create a timer 
    private int delay = 10;
    protected Timer timer = new Timer(delay, new TimerListener());
    
    /*
     * This is the constructor method for class frontend_main
     * 
     */
    public frontend_main(){
    	timer.start();
    }
    private class TimerListener implements ActionListener{
    	public void actionPerformed(ActionEvent e){
    		Collision();
    	}
    }
    
    private class mouseStuff implements MouseInputListener
    {
    	private int xpos;
    	private int ypos;
    	
		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			System.out.println("Claicked");
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
			inside = true;
			System.out.println(inside);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			inside = false;
			System.out.println(inside);
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			System.out.println("Pressed");
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			System.out.println("Released");
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
			
			//xpos=ballPanel.getX();
			//ypos=ballPanel.getY();
			//System.out.println(xpos+" "+ypos);
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
    	
    }
   
    
	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(485, 369);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("YingYang Bipolorization");
			JFrame aboutDialog = getConsoleFrame();
			aboutDialog.setVisible(false);
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getBallPanel(), null);
			jContentPane.add(getAdd00(), null);
			jContentPane.add(getAdd01(), null);
			jContentPane.add(getAdd10(), null);
			jContentPane.add(getAdd11(), null);
			jContentPane.add(getClearButton(), null);
			jContentPane.add(getShowConsoleBtn(), null);
			
		}
		return jContentPane;
	}

	

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getEditMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getSaveMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.add(getCutMenuItem());
			editMenu.add(getCopyMenuItem());
			editMenu.add(getPasteMenuItem());
		}
		return editMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//JFrame aboutDialog = getAboutDialog();
					//aboutDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(331, 260);
					consoleFrame.setLocation(loc);
					consoleFrame.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes consoleFrame	
	 * 	
	 * @return javax.swing.JDialog
	 */
	private JFrame getConsoleFrame() {
		if (consoleFrame == null) {
			consoleFrame = new JFrame();
			consoleFrame.setTitle("Console");
			consoleFrame.setSize(331, 260);
			consoleFrame.setContentPane(getAboutContentPane());
		}
		return consoleFrame;
	}

	/**
	 * This method initializes aboutContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAboutContentPane() {
		if (aboutContentPane == null) {
			aboutContentPane = new JPanel();
			aboutContentPane.setLayout(null);
			aboutContentPane.add(getConsoleArea(), null);
		}
		return aboutContentPane;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCutMenuItem() {
		if (cutMenuItem == null) {
			cutMenuItem = new JMenuItem();
			cutMenuItem.setText("Cut");
			cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
					Event.CTRL_MASK, true));
		}
		return cutMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
					Event.CTRL_MASK, true));
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
					Event.CTRL_MASK, true));
		}
		return pasteMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
		}
		return saveMenuItem;
	}

	/**
	 * This method initializes ballPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private CircleDraw getBallPanel(){
		
		if (ballPanel == null) {
			//ballPanel = new JPanel();
			ballPanel = new CircleDraw();
			System.out.println("test2");
			ballPanel.setLayout(null);
			ballPanel.setBounds(new Rectangle(6, 50, 458, 252));
			ballPanel.setBackground(Color.white);
			ballPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			ballPanel.setForeground(Color.white);
			ballPanel.addMouseListener(new mouseStuff());
		}
		return ballPanel;
		
		
	}

	/**
	 * This method initializes add00	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAdd00() {
		if (add00 == null) {
			add00 = new JButton("Add (0,0)");
			add00.setBounds(new Rectangle(8, 6, 100, 18));
			add00.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Random x = new Random(); 
					Random y = new Random(); 
					int tempx = x.nextInt(ballPanel.getWidth()-50);
					int tempy = y.nextInt(ballPanel.getHeight()-50);
					ballList.add(new Ball(tempx,tempy,"(0,0)",Color.blue));
					ballPanel.paintBalls(ballPanel.getGraphics(), ballList);
				}
			});
		}
		return add00;
	}

	/**
	 * This method initializes add01	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAdd01() {
		if (add01 == null) {
			add01 = new JButton("Add (0,+1)");
			add01.setBounds(new Rectangle(110, 6, 100, 18));
			add01.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Random x = new Random(); 
					Random y = new Random(); 
					int tempx = x.nextInt(ballPanel.getWidth()-50);
					int tempy = y.nextInt(ballPanel.getHeight()-50);
					ballList.add(new Ball(tempx,tempy,"(0,+1)",Color.red));
					ballPanel.paintBalls(ballPanel.getGraphics(), ballList);
				}
			});
			
		}
		return add01;
	}

	/**
	 * This method initializes add10	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAdd10() {
		if (add10 == null) {
			add10 = new JButton("Add (-1,0)");
			add10.setBounds(new Rectangle(8, 27, 100, 18));
			add10.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Random x = new Random(); 
					Random y = new Random(); 
					int tempx = x.nextInt(ballPanel.getWidth()-50);
					int tempy = y.nextInt(ballPanel.getHeight()-50);
					ballList.add(new Ball(tempx,tempy,"(-1,0)",Color.green));
					ballPanel.paintBalls(ballPanel.getGraphics(), ballList);
				}
			});
		}
		return add10;
	}

	/**
	 * This method initializes add11	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAdd11() {
		if (add11 == null) {
			add11 = new JButton();
			add11.setBounds(new Rectangle(110, 27, 100, 18));
			add11.setText("Add (-1,+1)");
			add11.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Random x = new Random(); 
					Random y = new Random(); 
					int tempx = x.nextInt(ballPanel.getWidth()-50);
					int tempy = y.nextInt(ballPanel.getHeight()-50);
					ballList.add(new Ball(tempx,tempy,"(-1,+1)",Color.gray));
					ballPanel.paintBalls(ballPanel.getGraphics(), ballList);
				}
			});
		}
		return add11;
	}
	
	//public void paint (Graphics g) {
	//    g2 = (Graphics2D) g;
	//}

	/**
	 * This method initializes clearButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getClearButton() {
		if (clearButton == null) {
			clearButton = new JButton();
			clearButton.setBounds(new Rectangle(337, 6, 125, 18));
			clearButton.setText("Clear Balls");
			clearButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ballList.clear();
					ballPanel.repaint();
					consoleArea.append("Cleared Balls\n");
				}
			});
		}
		return clearButton;
	}

	/**
	 * This method initializes consoleArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getConsoleArea() {
		if (consoleArea == null) {
			consoleArea = new JTextArea();
			consoleArea.setBounds(new Rectangle(7, 4, 303, 215));
			consoleArea.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		}
		return consoleArea;
	}

	/**
	 * This method initializes showConsoleBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getShowConsoleBtn() {
		if (showConsoleBtn == null) {
			showConsoleBtn = new JButton();
			showConsoleBtn.setBounds(new Rectangle(337, 27, 125, 18));
			showConsoleBtn.setText("Show Console");
			showConsoleBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					consoleFrame.setVisible(true);
				}
			});
		}
		return showConsoleBtn;
	}
	
	 /**
     * This method checks for collisions
     * 
     * @return
     */
    public  boolean Collision(){
    	//variables to compare distance between circle's centers
    	int dx = 50;
    	int dy = 50;
    	boolean coll = false;
    	//compare distance
    	if(ballList.size() == 1){//if only one ball, return false
    		return false;
    	}
    	else
    	for(int i = 0; i < ballList.size() -1; i++){
    		for(int j = 1; j < ballList.size(); j++){
    			if (dx > Math.abs((ballList.get(i).getCx() - ballList.get(j).getCx()))){
    				if(dy > Math.abs((ballList.get(i).getCy() - ballList.get(j).getCy()))){
    					coll = true;
    					
    				}
    			}
    		}
    	}
    	return coll;
    
    
    
    
    
    
    
    }

	/**
	 * Launches this application
	 */
 
    
    
    public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frontend_main application = new frontend_main();
				application.getJFrame().setVisible(true);
			}
		});
	}
}
